using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;
namespace DAL
{
  public  class TinhTrangGiaoHangDAL
    {
            private SqlConnection cn = null;
        public TinhTrangGiaoHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TinhTrangGiaoHang ttgh )
        {
            SqlCommand cm = new SqlCommand("TinhTrangGiaoHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
           
           cm.Parameters.AddWithValue("@TenTTGH",ttgh.tenttgh);
            cm.ExecuteNonQuery();
        }
        public void Update(TinhTrangGiaoHang ttgh)
        {
            SqlCommand cm = new SqlCommand("TinhTrangGiaoHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTTGH", ttgh.mattgh);
            cm.Parameters.AddWithValue("@TenTTGH",ttgh.tenttgh);
           
          cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TinhTrangGiaoHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTTGH", iD);
            cm.ExecuteNonQuery();
        }

        public tbTinhTrangGiaoHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TinhTrangGiaoHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTinhTrangGiaoHangCollection ttghCollection = new tbTinhTrangGiaoHangCollection();

            while (dr.Read())
            {
                TinhTrangGiaoHang tinhtranggiaohang = new TinhTrangGiaoHang();
                tinhtranggiaohang.mattgh = int.Parse(dr.GetValue(0).ToString());
                tinhtranggiaohang.tenttgh = dr.GetValue(1).ToString();

            
                ttghCollection.Add(tinhtranggiaohang);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ttghCollection;
        }
    }
    
}
